package com.situ.day8;

import org.junit.Test;

import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo {

    @Test
    public void test77() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生的数量： ");
        int count = scanner.nextInt();
        Student[] students = new Student[count];
        for (int i = 0; i < students.length; i++) {
            Student student = new Student();
            System.out.println("请输入id：");
            int id = scanner.nextInt();
            student.setId(id);
            System.out.println("请输入名字： ");
            String name = scanner.next();
            student.setName(name);

            students[i] = student;
        }
        // 全部输入完成，再打印素有学生信息
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            // System.out.println(student.toString());
            // 直接打印student对象，默认调用toString()方法
            System.out.println(student);
            // System.out.println(student.id);
            // System.out.println(student.name);
        }

        while (true) {
            System.out.println("输入1、按照学生id搜索");
            System.out.println("输入2、按照学生name搜索");
            System.out.println("输入3、按照学生age搜索");
            System.out.println("输入0、退出搜索");
            int type = scanner.nextInt();
            if (type == 0) {
                System.out.println("退出程序");
                break;
            }
            // 找到了没有，false，因为还没有开始找所以没有找到
            boolean isSearchFound = false;
            switch (type) {
                case 1:
                    System.out.println("请输入要搜索的id：");
                    int id = scanner.nextInt();
                    for (int i = 0; i < students.length; i++) {
                        Student student = students[i];
                        if (id == student.getId()) {
                            System.out.println(student);
                            isSearchFound = true;
                        }
                    }
                    /*if (!isSearchFound) {
                        System.out.println("没有该信息的学生");
                    }*/
                    if (isSearchFound == false) {
                        System.out.println("没有该信息的学生");
                    }
                    break;
                case 2:
                    System.out.println("请输入要搜索的name：");
                    String name = scanner.next();
                    for (int i = 0; i < students.length; i++) {
                        Student student = students[i];
                        if (name.equals(student.getName())) {
                            System.out.println(student);
                            isSearchFound = true;
                            // break;
                        }
                    }
                    if (isSearchFound == false) {
                        System.out.println("没有该信息的学生");
                    }

                    break;
            }
        }

        // 搜索要求：以根据学生name搜索为例，如果找到了符合这个名字的学生，就打印该学生所有信息
        // 如果没有找到这个name的学生，输出“没有该信息的学生”
        System.out.println("-------------------");
    }

    @Test
    public void test44() {
        Scanner scanner = new Scanner(System.in);
        List<Student> list = new ArrayList<>();
        while (true) { // for (;;) {}
            System.out.println("请输入id：");
            int id = scanner.nextInt();
            System.out.println("请输入名字： ");
            String name = scanner.next();
            System.out.println("请输入年龄：");
            int age = scanner.nextInt();
            System.out.println("请输入性别：");
            String gender = scanner.next();

            Student student = new Student(id, name, age, gender);
            list.add(student);

            System.out.println("您是否要继续输入：0退出，1继续");
            int quit = scanner.nextInt();
            if (quit == 0) {
                break;
            }
        }

        for (Student student : list) {
            System.out.println(student);
        }

        while (true) {
            System.out.println("输入1、按照学生id搜索");
            System.out.println("输入2、按照学生name搜索");
            System.out.println("输入3、按照学生age搜索");
            System.out.println("输入0、退出搜索");
            int type = scanner.nextInt();
            if (type == 0) {
                System.out.println("退出程序");
                break;
            }
            // 找到了没有，false，因为还没有开始找所以没有找到
            boolean isSearchFound = false;
            switch (type) {
                case 1:
                    System.out.println("请输入要搜索的id：");
                    int id = scanner.nextInt();
                    for (Student student : list) {
                        if (id == student.getId()) {
                            System.out.println(student);
                            isSearchFound = true;
                        }
                    }
                    /*if (!isSearchFound) {
                        System.out.println("没有该信息的学生");
                    }*/
                    if (isSearchFound == false) {
                        System.out.println("没有该信息的学生");
                    }
                    break;
                case 2:
                    System.out.println("请输入要搜索的name：");
                    String name = scanner.next();
                    for (Student student : list) {
                        if (name.equals(student.getName())) {
                            System.out.println(student);
                            isSearchFound = true;
                        }
                    }
                    if (isSearchFound == false) {
                        System.out.println("没有该信息的学生");
                    }

                    break;
            }
        }

        // 搜索要求：以根据学生name搜索为例，如果找到了符合这个名字的学生，就打印该学生所有信息
        // 如果没有找到这个name的学生，输出“没有该信息的学生”
        System.out.println("-------------------");
    }
}
